package bjfuwjx.exp5;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;

public class Client extends JFrame{
    public static JButton jenter=new JButton("进入聊天室");
    public static JButton jexit=new JButton("退出聊天室");
    public static JButton jsend=new JButton("发送");
    public static JTextPane t3=new JTextPane();
    public static JTextArea jt=new JTextArea();
    public static JTextArea msgbox=new JTextArea();

    public void showUI(){
        this.setTitle("Client");
        this.setSize(new Dimension(700,500));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        JPanel jp=new JPanel();
        jp.setBackground(Color.WHITE);
        this.add(jp);
        Dimension d=new Dimension(100,25);
        Container c1=new Container();
        c1.setVisible(true);
        c1.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
        JLabel l1=new JLabel("IP:");
        c1.add(l1);
        JTextPane t1=new JTextPane();
        t1.setPreferredSize(d);
        t1.setText("127.0.0.1");
        t1.setEnabled(false);
        c1.add(t1);
        JLabel l2=new JLabel("端口:");
        c1.add(l2);
        JTextPane t2=new JTextPane();
        t2.setPreferredSize(d);
        t2.setText("2428");
        t2.setEnabled(false);
        c1.add(t2);
        JLabel l3=new JLabel("昵称:");
        c1.add(l3);
        t3.setPreferredSize(d);
        c1.add(t3);
        jenter.setEnabled(true);
        c1.add(jenter);
        jexit.setEnabled(false);
        c1.add(jexit);
        this.add(c1,BorderLayout.PAGE_START);
        Container c2=new Container();
        c2.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
        jt.setPreferredSize(new Dimension(600,100));
        jt.setLineWrap(true);
        c2.add(jt);
        jsend.setEnabled(false);
        jsend.setPreferredSize(new Dimension(70,100));
        c2.add(jsend);
        this.add(c2,BorderLayout.PAGE_END);
        msgbox.setEditable(false);
        msgbox.setBackground(Color.WHITE);
        msgbox.setPreferredSize(new Dimension(700,350));
        msgbox.setLineWrap(true);
        this.add(msgbox,BorderLayout.CENTER);
        this.setVisible(true);
    }
    public static void main(String[] args) throws IOException {
        Client c=new Client();
        c.showUI();
        jenter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jenter.setEnabled(false);
                jexit.setEnabled(true);
                jsend.setEnabled(true);
                isRunning=true;
                try {
                    s = new Socket(CONNSTR, CONNPOST);
                    isRunning = true;
                } catch (SocketException ee) {
                    System.out.println("找不到服务器!");
                    ee.printStackTrace();
                }
                catch (IOException ee) {
                    ee.printStackTrace();
                }
                new Thread(new CReceive(s)).start();
                send(t3.getText(),"欢迎 "+t3.getText()+" 进入聊天室",s);
            }
        });
        jexit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jenter.setEnabled(true);
                jexit.setEnabled(false);
                jsend.setEnabled(false);
                System.exit(0);
            }
        });
        jsend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String strSend = jt.getText();
                if (strSend.trim().length() == 0) {
                    return;
                }
                send(t3.getText(),strSend,s);
                jt.setText("");
            }
        });
    }
    private static final String CONNSTR = "localhost";
    private static final int CONNPOST = 7608;
    private static Socket s=null;
    private static DataOutputStream dos = null;
    private static boolean isRunning = false;
    public static void send(String name,String str,Socket s) {
        try {
            dos=new DataOutputStream(s.getOutputStream());
            dos.writeUTF(name+" "+str);
            dos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    static class CReceive implements Runnable {
        private Socket s;
        public CReceive(Socket s){
            this.s=s;
        }
        @Override
        public void run() {
            try {
                while (isRunning) {
                    DataInputStream dis=new DataInputStream(s.getInputStream());
                    String str=dis.readUTF();
                    msgbox.append(str);
                    if (str.equals("昵称冲突，请更改昵称后重新进入\n")||str.equals("您已被踢出聊天室\n")){
                        jsend.setEnabled(false);
                        //System.exit(0);
                        break;
                    }
                }
            } catch (SocketException e) {
                msgbox.append("聊天室已关闭!\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
